package com.breaktian.assemble.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * 软键盘工具类
 * <p>
 * Created by An on 17/2/23.
 */
public final class KeyboardUtil {

    private KeyboardUtil() {
    }

    /**
     * 弹出软键盘
     *
     * @param context  Context
     * @param editText 编辑框EditText
     */
    public static void open(@NonNull Context context, @NonNull EditText editText) {
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(editText, 0);
    }

    /**
     * 关闭软键盘
     *
     * @param context  Context
     * @param editText 编辑框EditText
     */
    public static void close(@NonNull Context context, @NonNull EditText editText) {
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    /**
     * 获取软键盘打开状态
     *
     * @param context Context
     * @return true，软键盘打开；false，软键盘关闭
     */
    public static boolean isShow(@NonNull Context context) {
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        return inputManager.isActive();
    }
    /**
     * 获取软键盘打开状态
     *
     * @param context Context
     * @return true，软键盘打开；false，软键盘关闭
     */
    public static boolean isShow(@NonNull Context context, EditText editText) {
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        return inputManager.isActive(editText);
    }


    /**
     * 判断软键盘是否弹出
     */
    public static boolean isSHowKeyboard(Context context, EditText editText) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
        if (imm.hideSoftInputFromWindow(editText.getWindowToken(), 0)) {
            return true;
            //软键盘已弹出
        } else {
            return false;
            //软键盘未弹出
        }
    }


}
